﻿@{
    bool enablePaging = ViewBag.DisablePaging == null || ViewBag.DisablePaging == false;
    bool populateOnLoad = ViewBag.PopulateOnLoad != null && ViewBag.PopulateOnLoad == true;
    string customColumnsDefinition = ViewBag.CustomColumns ?? "";
}

<div align="center">
    <table id='@(ViewBag.GridName)' style="width: 100%"></table>
    @if (enablePaging)
    {<div id="@(ViewBag.GridName)_pager"></div>}
</div>

<script type="text/javascript">
    
    function gridColumnConnected_@(ViewBag.GridName)(cellvalue, options, rowobject) {
        var connected = rowobject.Connected;
        return "<img src='/Images/" + (connected ? "Online.png" : "Offline.png") + "' title='" + (connected ? "@(Resources.Intercamber.Connected)" : "@(Resources.Intercamber.Disconnected)") + "'  />";
    }
    function gridColumnName_@(ViewBag.GridName)(cellvalue, options, rowobject) {
        var label = rowobject.Name, id = rowobject.IdUser;
        if(typeof profilViewerManager.showProfil !== "undefined")
            return "<label onclick='profilViewerManager.showProfil(" + id + ");' class='link'>" +label+ "<label>";
        return label;
    }
    function gridExpandFormatter_@(ViewBag.GridName)(subgridId, rowId) {
        var userInfos = $("#@(ViewBag.GridName)").getRowData(rowId);
        var dest = $("#" + subgridId);
        dest.css("background-color", "white");
        dest.css("padding-left", "48px");

        var desc = userInfos.PresentationText;
        if (desc == "") desc = "@Resources.Intercamber.NoPresentation";
        dest.html(desc);
    }

    $(function () {
        $("#@(ViewBag.GridName)").jqGrid({
            //autowidth: true,
            datatype: 'json',
            width: '100%',
            height: '100%',
            jsonReader: { 'repeatitems': false, 'id': 'IdUser' },
            loadonce: @(enablePaging ? "false" : "true"),
            @if (enablePaging)
            {
                <text>
            pager: '#@(ViewBag.GridName)_pager',
            rowList: [5, 10, 15],
            rowNum: 5,
                </text>
            }
            url: '@(ViewBag.GridUrl)',
            viewrecords: true,
            colModel: [
                @Html.Raw(customColumnsDefinition)
                {   name: 'Name', label: '@Resources.Intercamber.SearchContacts_ColName', formatter:gridColumnName_@(ViewBag.GridName) },
                {   align: 'center', name: 'Age', fixed: true, label: '@Resources.Intercamber.SearchContacts_ColAge', width: 70 }, 
                {   name: 'Location', label: '@Resources.Intercamber.SearchContacts_ColLocation', index: 'Location'}, 
                {   name: 'Connected', fixed: true, formatter: gridColumnConnected_@(ViewBag.GridName), align: 'center', 
                    label: '@Resources.Intercamber.SearchContacts_ColConnected', width: 90, index: 'Connected'
                }, 
                { name: 'PresentationText', hidden: true }
            ],
            subGrid: true,
            subGridRowExpanded: gridExpandFormatter_@(ViewBag.GridName),
            subGridOptions: { expandOnLoad: true, selectOnExpand: false, reloadOnExpand: false },
            loadComplete: function () {
                $("#@(ViewBag.GridName)").jqGrid("hideCol", "subgrid");
                $("#@(ViewBag.GridName)").jqGrid('setGridWidth', 800, true);
            },
        })
        @if (populateOnLoad) { Html.Raw(".trigger(\"reloadGrid\", [{ page: 1 }])"); };
    })
</script>
